$().ready(function() {
    $(".registerbody").validate({
        rules:{
            Uname:{
                required:true,
                rangelength:[11,11],
            },
            webforminput:{
                required:true,
            },
            Upwd:{
                rangelength:[6,16],
                required:true,
            },
            Upwd2:{
                equalTo:"#Upwd"
            }
        },
        messages:{
            Uname:{
                required:"请输入邮箱账号",
                rangelength:"请输入正确格式手机号",
            },
            webforminput:{
                required:"请输入验证码"
            },
            Upwd:{
                rangelength:"密码长度必须在6-16位之间",
                required:"请输入密码"
            },
            Upwd2:{
                equalTo:"2次密码不一致"
            }
        },
        errorPlacement: function(error, element) {
            // Append error within linked label
            error.insertAfter(element);
        },
        submitHandler:function(form){
            alert("提交事件!");
            form.submit();
        }
    });
});

function submit() {
    // console.log($(".m-loginBox .mail-login [name = Uname]").val())
    // console.log($(".m-loginBox .mail-login [name = Upwd]").val())
    $.ajax({
        url : "http://localhost/jiayouhui/php/register.php",
        type : "post",
        data : {
            Uname : $(".registerbody [name = Uname]").val(),
            Upwd : $(".registerbody [name = Upwd]").val()
        },
        dataType : "json",
        success : function (data) {
            var $result = data;
            console.log($result.status);
            if ($result.status == 1){
                alert("注册成功！");
                window.location.href='http://localhost/jiayouhui/index.html';
            }
            else if ($result.status == 0){
                // <label id="Upwd-error" class="error" for="Upwd">请输入密码</label>
                if ($("#account-error").length <= 0) {
                    $error = $("<label class=\"account-error\">注册失败,该用户已经存在！</label>")
                    $error.insertAfter($(".registerbody"))
                }
            }
        },
        error : function () {
            console.log("sql error")
        }
    })
}
